package com.blb.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.blb.ssm.dao.UserMapper;
import com.blb.ssm.entity.User;
import com.blb.ssm.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;

	@Transactional(rollbackFor=Exception.class)
	@Override
	public User login(String username, String password) {
		User user =userMapper.login(username);
		if (user!=null) {
			if (user.getPassword().equals(password)) {
			}
			else{
				user = null;
			}
		}
		return user;
		
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public int getcount(String uname, String username) {
		// TODO Auto-generated method stub
		return userMapper.getcount(uname,username);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public String adduser(User user) {
		// TODO Auto-generated method stub
		User user2 = userMapper.selectUserByUsername(user.getUsername());
		if (user2 !=null) {
			return "该名称已经存在!";
		} else {
			userMapper.addUser(user);
			return "添加成功";
		}
		
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deluser(String uid) {
		// TODO Auto-generated method stub
		userMapper.delUser(uid);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public List<User> getlist(String uname, String username, int page, int limit) {
		int start=(page-1)*limit;
		return userMapper.getlist(uname,username,start,limit);

	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public String updateuser(User user) {
		User user2=userMapper.selectUserByUsername(user.getUsername());
		String uid = userMapper.selectUserByUID(user.getUsername());
		if(user2 !=null && !uid.equals(user.getUid())){
			return "该用户名称已经存在!";
		}else{
			userMapper.updateUser(user);
			return "用户编辑成功!";
		}
	}
}
